<template>
  <el-dialog :title="form.unitId ? '编辑' : '新增'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="选择楼宇" prop="buildingId">
            <el-select v-model="form.buildingId">
              <el-option
                  v-for="build in buildingList"
                  :key="build.buildingId"
                  :label="build.buildingName"
                  :value="build.buildingId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="单元名称" prop="unitName">
            <el-input v-model="form.unitName" placeholder="请输入单元名称"/>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="PropertyUnitDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj, getBuildingList } from '/@/api/unit/unit'
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)

const buildingList = ref([])

// 提交表单数据
const form = reactive({
  unitId:'',
  unitName: '',
  buildingId: '',
  remark: '',
});

// 定义校验规则
const dataRules = ref({
  unitName: [{required: true, message: '单元名称不能为空', trigger: 'blur'}],
  buildingId: [{required: true, message: '请选择楼宇', trigger: 'change'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.unitId = ''

  // 加载楼宇列表
  loadBuildingList()

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取propertyUnit信息
  if (id) {
    form.unitId = id
    getpropertyUnitData(id)
  }
};

// 加载楼宇列表
const loadBuildingList = async () => {

    const res = await getBuildingList()
    buildingList.value = res.data
}

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) return false;

  try {
    loading.value = true;
    form.unitId ? await putObj(form) : await addObj(form);
    useMessage().success(form.unitId ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};

// 初始化表单数据
const getpropertyUnitData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
